package com.hilihili.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;

/**
 * 视频转码队列配置
 *
 * @author machi
 * @date 2024/01/21
 */
@Configuration

public class VideoTranscodeMqConfig {
    @Autowired
    RabbitAdmin rabbitAdmin;
    /**
     * 小文件处理队列
     */
    public static final String DIRECT_VIDEO_TRANSCODE_SMALL_FILE_QUEUE = "directVideoTranscodeSmallFileQueue";
    /**
     * 大文件处理队列
     */
    public static final String DIRECT_VIDEO_TRANSCODE_LARGE_FILE_QUEUE = "directVideoTranscodeLargeFileQueue";
    /**
     * 消息发放交换机
     */
    public static final String DIRECT_VIDEO_TRANSCODE_EXCHANGE = "directVideoTranscodeExchange";
    /**
     * 小文件路由标识
     */
    public static final String DIRECT_VIDEO_TRANSCODE_SMALL_FILE_QUEUE_ROUTING_KEY = "small";
    /**
     * 大文件路由标识
     */
    public static final String DIRECT_VIDEO_TRANSCODE_LARGE_FILE_QUEUE_ROUTING_KEY = "large";

    @Bean
    public Queue directVideoTranscodeSmallFileQueue() {
        /**
         * 1、name:    队列名称
         * 2、durable: 是否持久化
         * 3、exclusive: 是否独享、排外的。如果设置为true，定义为排他队列。则只有创建者可以使用此队列。也就是private私有的。
         * 4、autoDelete: 是否自动删除。也就是临时队列。当最后一个消费者断开连接后，会自动删除。
         * */
        return new Queue(VideoTranscodeMqConfig.DIRECT_VIDEO_TRANSCODE_SMALL_FILE_QUEUE, true, false, false);
    }

    @Bean
    public Queue directVideoTranscodeLargeFileQueue() {
        return new Queue(VideoTranscodeMqConfig.DIRECT_VIDEO_TRANSCODE_LARGE_FILE_QUEUE, true, false, false);
    }

    @Bean
    public DirectExchange directVideoTranscodeExchange() {
        //Direct交换机
        return new DirectExchange(VideoTranscodeMqConfig.DIRECT_VIDEO_TRANSCODE_EXCHANGE, true, false);
    }


    @Bean
    public Binding bindSmall() {
        // 将对列绑定到交换机中
        return BindingBuilder
                .bind(directVideoTranscodeSmallFileQueue())
                .to(directVideoTranscodeExchange())
                .with(VideoTranscodeMqConfig.DIRECT_VIDEO_TRANSCODE_SMALL_FILE_QUEUE_ROUTING_KEY);
    }

    @Bean
    public Binding bindLarge() {

        return BindingBuilder
                .bind(directVideoTranscodeLargeFileQueue())
                .to(directVideoTranscodeExchange())
                .with(VideoTranscodeMqConfig.DIRECT_VIDEO_TRANSCODE_LARGE_FILE_QUEUE_ROUTING_KEY);
    }

    //@PostConstruct 当前类创建完成后执行的方法
    @PostConstruct
    public void init() {
        rabbitAdmin.declareQueue(directVideoTranscodeLargeFileQueue());
        rabbitAdmin.declareQueue(directVideoTranscodeSmallFileQueue());
        rabbitAdmin.declareExchange(directVideoTranscodeExchange());
    }
}
